package com.bobo.controller;

import com.bobo.model.MonteCarloPIData;
import com.bobo.view.AlgoFrame;
import com.bobo.model.Circle;
import com.bobo.units.AlgoVisUnit;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

/**
 * @Author: bobobo
 * @Date: 2019/9/3 11:34
 * @Version：1.0
 */
public class AlgoVisualizer {
	private static int DELAY = 40;
    //创建自己的数据
    private MonteCarloPIData data;
    private int N;
    private AlgoFrame frame;   //视图

    public AlgoVisualizer(int sceneWidth,int sceneHeight,int N){
        // 初始化自己的数据
        //初始化数据
        this.N = N;

        if (sceneHeight != sceneWidth)
            throw new IllegalArgumentException("This demo must be run in a square window");

        Circle circle = new Circle(sceneWidth/2,sceneHeight/2,sceneHeight/2);
        data = new MonteCarloPIData(circle);

        //初始化视图
        // 事件队列 创建新线程===事件分发线程
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Get PI with MonteCarlo",sceneWidth,sceneHeight);

            new Thread(() -> {
                run();
            }).start();
        });
    }

    //动画逻辑
    private void run(){
        // 编写自己的动画逻辑
        for (int i = 0;i < N;i++){

            if (i % 100 == 0){
                frame.render(data);
                AlgoVisUnit.pause(DELAY);
                System.out.println(data.getPI());
            }

            int x = (int) (Math.random() * frame.getCanvasWidth());
            int y = (int) (Math.random() * frame.getCanvasHeight());

            data.addPoint(new Point(x,y));
        }
    }

}
